package uts.sdk.modules.oneoilNewland;

import android.content.Context;
import android.os.Bundle;
import android.os.RemoteException;
import com.newland.aidl.printer.AidlPrinter;
import com.newland.aidl.printer.AidlPrinterListener;
import com.newland.aidl.printer.PrinterCode;
import uts.sdk.modules.oneoilNewland.R;
import uts.sdk.modules.oneoilNewland.NewlandService;
import uts.sdk.modules.oneoilNewland.ICallback;
import uts.sdk.modules.oneoilNewland.Response;

public class PrinterAction {
	private AidlPrinter printer;
	
	private Bundle format;
	
    private PrinterAction() {
		this.printer = NewlandService.getPrinter();
		this.format = new Bundle();
	}
    
    // 静态内部类不会在外部类加载时立即加载
    private static class PrinterActionHolder {
        private static final PrinterAction INSTANCE = new PrinterAction();
    }
    
    // 第一次调用时才会加载SingletonHolder类
    public static PrinterAction getInstance() {
        return PrinterActionHolder.INSTANCE;
    }
	
	public String getStatusString() {
		try {
			String status;
			switch (printer.getStatus()) {
				case PrinterCode.PrinterState.PRINTER_NORMAL: // 正常
					// status = "PRINTER_NORMAL";
					status = "正常";
					break;
				case PrinterCode.PrinterState.PRINTER_OUTOF_PAPER: // 缺纸
					// status = "PRINTER_OUTOF_PAPER";
					status = "缺纸";
					break;
				case PrinterCode.PrinterState.PRINTER_HEAT_LIMITED: // 超温
					// status = "PRINTER_HEAT_LIMITED";
					status = "超温";
					break;
				case  PrinterCode.PrinterState.PRINTER_BUSY: // 打印机忙
					// status = "PRINTER_BUSY";
					status = "打印机忙";
					break;
				default:
					// status = "PRINTER_ERROE_OTHER"; // 其他错误
					status = "其他错误";
					break;
			}
			return status;
		} catch (Exception e) {
			e.printStackTrace();
			return e.toString();
		}
	}
	
	public void initPrinter() {
		this.format = new Bundle();
	}
	
	public int getStatus() throws RemoteException {
		return this.printer.getStatus();
	}
	
	// public void setWidth(int width) {
	// 	this.format.putInt("width", width);
	// }
	// public void setHeight(int height) {
	// 	this.format.putInt("height", height);
	// }
	// public void setAlign(String align) {
	// 	this.format.putString("align", align);
	// }
	// public void setFont(String font) {
	// 	this.format.putString("font", font);
	// }
	// public void setHzFont(int font) {
	// 	this.format.putInt("hzFont", font);
	// }
	// public void setZmFont(int font) {
	// 	this.format.putInt("zmFont", font);
	// }
	// public void setZoom(int zoom) {
	// 	this.format.putInt("zoom", zoom);
	// }
	// public void setOffset(int offset) {
	// 	this.format.putInt("offset", offset);
	// }
	// public void setLineFeed(boolean linefeed) {
	// 	this.format.putBoolean("linefeed", linefeed);
	// }
	

	
	// public void setGray(int var1) throws RemoteException {
	// 	this.printer.setGray(var1);
	// }
	// public void setPaperSize(int var1) throws RemoteException {
	// 	this.printer.setPaperSize(var1);
	// }
	// public void setSpace(int var1) throws RemoteException {
	// 	this.printer.setSpace(var1);
	// }
	// public void paperSkip(int var1) throws RemoteException {
	// 	this.printer.paperSkip(var1);
	// }
	

	
	public void addText(Bundle format,String text) throws RemoteException {
		this.printer.addText(format, text);
	}
	public void addBarCode(Bundle format,String barCode) throws RemoteException {
		this.printer.addBarCode(this.format, barCode);
	}
	public void addQrCode(Bundle format, String qrCode) throws RemoteException {
		this.printer.addQrCode(format, qrCode);
	}
	public boolean papercut() throws RemoteException {
		return this.printer.Papercut();
	}
	
	public void startPrinter(ICallback<String> callback) throws RemoteException {
		
				
				       
		this.printer.startPrinter(new AidlPrinterListener.Stub() {
            @Override
            public void onFinish() throws RemoteException {
				papercut();
                callback.execute(new Response<String>(1, "", ""));
            }
            @Override
            public void onError(int arg0, String arg1) throws RemoteException {
				callback.execute(new Response<String>(1, String.valueOf(arg0), arg1));
            }
        });
	}
}